package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.user.CreateUserBo;
import com.zhuss.bean.bo.core.user.CreateUserRoleBo;
import com.zhuss.bean.bo.core.user.UserDeptBindBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.core.db.entity.Dept;
import com.zhuss.core.db.entity.User;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.PwdUtil;

/**
 * 创建用户 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("userCreate")
public class UserCreate extends AbstractProcessor<Object, CreateUserBo> implements Processor<Object, CreateUserBo> {

    @Autowired
    private UserDao userDao;
    
    @Autowired
	@Qualifier("userRead")
	private UserRead userRead;
    
    @Autowired
	@Qualifier("userDeptBind")
	private Processor<Object, UserDeptBindBo> userDeptBind;
    
    @Autowired
	@Qualifier("deptRead")
	private DeptRead deptRead;
    
    @Autowired
	@Qualifier("userRoleCreate")
	private Processor<Object, CreateUserRoleBo> userRoleCreate;

    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateUserBo bo) throws AabException {
    	String username = bo.getUsername();
    	String name = bo.getName();
    	String pwd = bo.getPwd();
    	Long companyId = bo.getCompanyId();
    	List<Long> deptIdList = bo.getDeptIdList();
    	String openId = bo.getOpenId();
    	if (openId == null || "".equals(openId.trim())) {
    		if (username == null || "".equals(username)) {
        		throw new AabException(ExceptionCode.用户账号为空);
        	}
        	if (name == null || "".equals(name)) {
        		throw new AabException(ExceptionCode.用户名字为空);
        	}
        	if (companyId == null || companyId.equals(0l)) {
        		throw new AabException(ExceptionCode.用户公司为空);
        	}
        	List<Dept> companyDeptList = deptRead.listByCompany(companyId);
        	if ((deptIdList == null || deptIdList.isEmpty())
        			&& !companyDeptList.isEmpty() && IsEnum.否.sameKey(bo.getIsBatch())) {
        		// throw new AabException(ExceptionCode.用户部门为空);
        	}
        	if (pwd == null || "".equals(pwd)) {
        		bo.setPwd(PwdUtil.md5(PwdUtil.init()));
        	}
        	
        	List<User> list = userRead.list(username);
        	if (!list.isEmpty()) {
        		throw new AabException(ExceptionCode.用户账号已存在);
        	}
    	}
    	
    }
    
    /**
     * 操作方法
     * @throws AabException 
     */
    @Override
    public Object process(CreateUserBo param) throws AabException {
    	User record = new User();
    	record.setAge(param.getAge());
    	if (IsEnum.是.sameKey(param.getIsBind())) {
    		record.setArea(param.getConcurrentArea());
    	}
    	record.setCompanyId(param.getCompanyId());
    	record.setIsLimitArea(param.getIsLimitArea());
    	record.setName(param.getName());
    	record.setOpenId(param.getOpenId());
    	record.setIsBind(param.getIsBind());
    	record.setPhone(param.getPhone());
    	record.setPwd(param.getPwd());
    	record.setSex(param.getSex());
    	record.setUsername(param.getUsername());
    	record.setType(param.getType());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	userDao.create(record);
    	
    	Long userId = record.getUserId();
    	
    	List<Long> deptIdList = param.getDeptIdList();
    	if (deptIdList != null && !deptIdList.isEmpty()) {
    		UserDeptBindBo bo = new UserDeptBindBo();
        	bo.setUserId(userId);
        	bo.setDeptIdList(deptIdList);
        	userDeptBind.process(bo);
    	}
    	
    	List<Long> roleIdList = param.getRoleIdList();
    	
    	if (roleIdList != null && !roleIdList.isEmpty()) {
    		for (Long roleId : roleIdList) {
        		CreateUserRoleBo createUserRoleBo = new CreateUserRoleBo();
        		BeanUtils.copyProperties(param, createUserRoleBo);
        		createUserRoleBo.setUserId(userId);
        		createUserRoleBo.setRoleId(roleId);
        		userRoleCreate.process(createUserRoleBo);
        	}
    	}
    	
    	
    	
    	param.setTargetId(userId);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateUserBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.用户创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
